using CfgTable;
using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.GamePlay
{
    public class ServiceUnitNonLevel : IReusableClass
    {
        private TViewEntity _host;
        private CfgNonLevelServiceUnit _cfgUnit;
        //private List<BaseNonLevelExecution> _metExecs = new();
        //private List<BaseNonLevelExecution> _unmetExecs = new();

        public uint MaxStore => 200;

        public void Init(TViewEntity host, int unitCid)
        {
            this._host = host;
            this._cfgUnit = TableCenter.nonLevelServiceUnit.Get(unitCid);
            //foreach (var cid in cfgUnit.MetExecution)
            //{
            //    var cfgExec = TableCenter.nonLevelExecution.Get(cid);
            //    var exec = MainCityServiceFactory.CreateExecution(cfgExec, unitCid);
            //    //有些是服务器处理的执行，客户端略过
            //    if (exec != null)
            //    {
            //        exec.InitHost(_host);
            //        _metExecs.Add(exec);
            //    }
            //}
            //foreach (var cid in cfgUnit.UnmetExecution)
            //{
            //    var cfgExec = TableCenter.nonLevelExecution.Get(cid);
            //    var exec = MainCityServiceFactory.CreateExecution(cfgExec, unitCid);
            //    //有些是服务器处理的执行，客户端略过
            //    if (exec == null)
            //    {
            //        exec.InitHost(_host);
            //        _unmetExecs.Add(exec);
            //    }
            //}
        }

        public int Execute()
        {
            var curArea = GameplayAreaManager.Instance.curSceneArea;
            if (curArea == null)
            {
                return 0;
            }
            (bool isOK, int failCid) = curArea.CheckCondition(_cfgUnit.ExecCondition, _host);
            //改为直接返回配置的单元列表长度，真正的表现执行等服务器通知
            if (isOK)
            {
                return _cfgUnit.MetExecution.Count;
            }
            else
            {
                return _cfgUnit.UnmetExecution.Count;
            }
        }

        //private int Execute(List<BaseNonLevelExecution> execs)
        //{
        //    foreach (var exec in execs)
        //    {
        //        exec.Execute();
        //    }
        //    return execs.Count;
        //}

        public void OnReset()
        {
            _host = null;
            _cfgUnit = null;
            //foreach (var exec in _metExecs)
            //{
            //    ClassPool.PutAutoType(exec);
            //}
            //_metExecs.Clear();
            //foreach (var exec in _unmetExecs)
            //{
            //    ClassPool.PutAutoType(exec);
            //}
            //_unmetExecs.Clear();
        }
    }
}
